//
//  TableViewController.m
//  Net2
//
//  Created by Justsoso on 16/3/28.
//  Copyright © 2016年 Justsoso. All rights reserved.
//

#import "TableViewController.h"
#import <UIImageView+WebCache.h>
#import "Video.h"
#import "NetManager.h"
#import "def.h"

@interface TableViewController ()

@property (nonatomic,strong) NSMutableArray *list;

@end

@implementation TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIRefreshControl *c = [[UIRefreshControl alloc] init];
    c.attributedTitle = [[NSAttributedString alloc] initWithString:@"图片列表"];
    [c addTarget:self  action:@selector(requestVideosList) forControlEvents:UIControlEventValueChanged];
    
    self.refreshControl = c;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestMovieListSuccess:) name:kNotificantionListSuccess object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestMovieListFail:) name:kNotificantionListFail object:nil];
}

- (void)requestVideosList
{
    
    NetManager *net = [NetManager shareNetManager];
    [net requestMovieList];
    [self.refreshControl endRefreshing];
}

- (void)requestMovieListSuccess:(NSNotification *)n
{
    NSArray *success = n.userInfo[@"list"];
    self.list = [NSMutableArray array];
    for (NSDictionary *d in success)
    {
        Video *v =[Video videoWithDict:d];
        [self.list addObject:v];
    }
    [self.tableView reloadData];
}

- (void)requestMovieListFail:(NSNotification *)n
{
    NSError *error = n.userInfo[@"error"];
    NSLog(@"错误信息:%@",error);
}
#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.list.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    
    // Configure the cell...
    
    Video *v = self.list[indexPath.row];
    
    cell.textLabel.text = v.name;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"时长:%i",v.length];
    NSString *imageUrlStr = [NSString stringWithFormat:@"http://localhost:8080/MJServer/%@",v.image];
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:imageUrlStr] placeholderImage:[UIImage imageNamed:@"女帝.jpg"]];
    
    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
